package com.youdianda.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youdianda.constant.MessageConstant;
import com.youdianda.dto.YddAdvimgPageQueryDTO;
import com.youdianda.dto.YddLinkDTO;
import com.youdianda.dto.YddLinkPageQueryDTO;
import com.youdianda.entity.YddLink;
import com.youdianda.mapper.YddLinkMapper;
import com.youdianda.result.Result;
import com.youdianda.service.IYddLinkService;
import com.youdianda.vo.YddAdvimgPageQueryVO;
import com.youdianda.vo.YddLinkPageQueryVO;
import com.youdianda.vo.YddLinkVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mosen
 * @since 2023-05-15
 */
@Service
public class YddLinkServiceImpl extends ServiceImpl<YddLinkMapper, YddLink> implements IYddLinkService {

    @Autowired
    private YddLinkMapper yddLinkMapper;

    /**
     * 友情链接详情根据id查询
     *
     * @param id
     * @return
     */
    @Override
    public Result<YddLinkVO> selectById(Integer id) {
        //1.非空判断
        if (id == null) {
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }

        //2.业务查询
        YddLink yddLink = yddLinkMapper.selectById(id);
        YddLinkVO yddLinkVO = new YddLinkVO();
        BeanUtils.copyProperties(yddLink, yddLinkVO);


        //3.响应数据
        return Result.success(yddLinkVO);

    }

    /**
     * 链接列表分页
     *
     * @param yddLinkPageQueryDTO
     * @return
     */
    @Override
    public Result<YddLinkPageQueryVO> pageQuery(YddLinkPageQueryDTO yddLinkPageQueryDTO) {
        //1.非空判断
        if (yddLinkPageQueryDTO == null) {
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }

        //2.业务判断
        LambdaQueryWrapper<YddLink> lqw = new LambdaQueryWrapper<>();
        String type = yddLinkPageQueryDTO.getType();

        if (type != null) {
            lqw.eq(YddLink::getType, type);
        }

        IPage<YddLink> page = new Page<>(yddLinkPageQueryDTO.getPage(), yddLinkPageQueryDTO.getLimit());

        yddLinkMapper.selectPage(page, lqw);

        //3.封装数据
        YddLinkPageQueryVO vo = YddLinkPageQueryVO.builder()
                .currentPage(page.getCurrent())
                .totalPages(page.getPages())
                .pageSize(page.getSize())
                .count(page.getTotal())
                .data(page.getRecords())
                .build();

        //4.响应数据
        return Result.success(vo);
    }

    /**
     * 修改友情链接详情
     *
     * @param yddLinkDTO
     * @return
     */
    @Override
    public Result updateYddLink(YddLinkDTO yddLinkDTO) {
        //1.非空判断
        if (yddLinkDTO == null) {
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }

        //2.业务逻辑
        YddLink yddLink = new YddLink();
        BeanUtils.copyProperties(yddLinkDTO, yddLink);
        yddLinkMapper.updateById(yddLink);

        //3.响应数据
        return Result.success();
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @Override
    public Result deleteById(Integer id) {
        //1.非空判断
        if (id == null) {
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }
        //2.业务判断
        yddLinkMapper.deleteById(id);

        return Result.success();
    }

    /**
     * 链接添加
     *
     * @param yddLinkDTO
     * @return
     */
    @Override
    public Result insertYddLink(YddLinkDTO yddLinkDTO) {
        //1.非空判断
        if (yddLinkDTO == null) {
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }

        //2.业务判断
        YddLink yddLink = new YddLink();
        BeanUtils.copyProperties(yddLinkDTO,yddLink);
        yddLinkMapper.insert(yddLink);

        //3.响应数据
        return Result.success();
    }


}
